package com.huruwo.serialporthelper.utils;

/*
 *  @项目名：  Elite
 *  @包名：    com.dlc.elite.serial.model
 *  @文件名:   HeartbeatParam
 *  @创建者:   高渊
 *  @创建时间:  2017/6/13 14:08
 *  @描述：
 */

import android.content.ContentValues;
import android.util.Log;

import static android.content.ContentValues.TAG;

public class HeartbeatParam {
    //长度匹配才正常,字节长度
    public int    dataLen;
    //数据类型为0x0A才正常
    public int    dateType;
    //设备状态 00正常 01异常
    public int    status;
    //体重kg
    public String weight;
    //体脂
    public String fat;
    //身高
    public String height;
    //阻抗
    public int    fst;
    //    //人脸识别的性别
    //    public int sexFace;
    //
    //    public int getSexFace() {
    //        return sexFace;
    //    }
    //
    //    public void setSexFace(int sexFace) {
    //        this.sexFace = sexFace;
    //    }

    @Override
    public String toString() {
        return "HeartbeatParam{"
                + "dataLen="
                + dataLen
                + ", dateType="
                + dateType
                + ", status="
                + status
                + ", weight='"
                + weight
                + '\''
                + ", fat='"
                + fat
                + '\''
                + ", height='"
                + height
                + '\''
                + ", fst="
                + fst
                //                + '\''
                //                + ", sexFace="
                //                + sexFace
                + '}';
    }

    /**
     * @param appdata
     */
    public HeartbeatParam(String appdata) {
        // EventBus.getDefault().post(new TTestEvent(appdata));

        dateType = (int) ByteUtil.hexStr2decimal(appdata.substring(8, 10));
        dataLen = (int) ByteUtil.hexStr2decimal(appdata.substring(10, 14));

        if (dateType == 0x0B && dataLen == 0x0B) {
            //数据段
            String data = appdata.substring(14);
            status = (int) ByteUtil.hexStr2decimal(data.substring(0, 2));
            weight = generic(data, 2, 4, 6);

            float aaa = Float.parseFloat(weight) / 2;
            //Log.d(TAG, "HeartbeatParam: aaa:" + aaa);

            weight = String.valueOf(aaa);
            fat = generic(data, 6, 8, 10);
            fst = Integer.parseInt(data.substring(6, 10), 16);
            height = generic(data, 10, 12, 14);
            //00 0B00  7F01 00000000 FFFFFFFF
        }
    }

    //-->000100000B000B00 00000000 0000FFFFFFFF
    //-->000100000B000B00 00000000 0000FFFFFFFF
    //-->000100000B000B00 AE000000 0000FFFFFFFF
    //-->000100000B000B00 AE000000 0000FFFFFFFF
    //-->000100000B000B00 AE000000 0000FFFFFFFF
    //-->000100000B000B00 AE000000 0000FFFFFFFF
    //-->000100000B000B00 AE000000 0000FFFFFFFF
    //-->000100000B000B00 B4080159 0000FFFFFFFF
    //-->000100000B000B00 B4080159 0000FFFFFFFF
    //-->000100000B000B00 B4080159 0000FFFFFFFF
    //-->000100000B000B00 B4080159 0000FFFFFFFF
    //-->000100000B000B00 B4080159 0000FFFFFFFF
    //-->000100000B000B00 00000000 0000FFFFFFFF
    //-->000100000B000B00 00000000 0000FFFFFFFF
    //-->000100000B000B00 00000000 0000FFFFFFFF


    private String generic(String data, int start, int split, int end) {
        int a = (int) ByteUtil.hexStr2decimal(data.substring(start, split));
        int b = (int) ByteUtil.hexStr2decimal(data.substring(split, end));

       // Log.d(TAG, "generic: a:" + a + "\tb:" + b);


        return Integer.toString(a) + "." + Integer.toString(b);
    }
}
